﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ATM_Pro.Module;
using System.Net;

using System.Threading;

namespace ATM_Pro
{
    public class WarnPacketHandle : IPacketHandler
    {
        public WarnPacketHandle()
        {
            wpb = new WarnPacketBean();
        }


        private WarnPacketBean wpb;
        public WarnPacketBean WarnPackBean
        {
            get { return wpb; }
        }

        public event ImageDataChangeHandle DataChange;

        #region IPacketHandler Members

        public bool CanHandle(byte[] bytes)
        {

            return (IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 8)) == 1);
        }

        int count = 0;
        int preMinute = 0;
        int handleCount = 0;
        int preWarnId = -1;
        public void Handle(byte[] bytes)
        {
            DateTime warnTime;
            int packType = -1;
            int warnId = -1;
            int warnType = -1;
            int warnLevel = -1;
            int atmId = -1;
            int camId = -1;
            int addId = -1;
            int length = -1;
            int imgLength = -1;

            length = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 4));//数据长度  first
            if (length+8 == bytes.Length)
            {
                packType = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 8));
                warnTime = GetLocalTime(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 12)));
                warnId = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 16));
                atmId = bytes[20];
                camId = bytes[21];
                addId = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(bytes, 22));
                warnType = bytes[24];
                warnLevel = bytes[25];
                imgLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 26));//实时图像长度

                if (imgLength < 0) return;
                

                ATM atm = new ATM(atmId, Enum.GetName(typeof(AtmAddress), addId), addId, new Camera(camId, Enum.GetName(typeof(CameraNumber), camId), PacketType.AlarmMessage));
                wpb.AtmBean = atm;
                wpb.WarnId = warnId;
                wpb.WarnLevel = warnLevel;
                wpb.WarnTime = warnTime;
                wpb.WarnType = warnType;
                wpb.ExceptImg = bytes;//没有截出图片
                wpb.ExceptImgLength = imgLength;
                wpb.PackLength = length;
                //string path = ImageHandle.GenerateSavePath(atm, wpb.WarnTime, count++, 2);
                
                if (preWarnId != warnId)
                {
                    string savePath = ImageHandle.GenerateSavePath(atm, wpb.WarnTime, count++, 2);
                    MemoryStream ms = new MemoryStream(bytes, 32, imgLength);
                    System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                    img.Save(savePath);
                    img.Dispose();
                    ms.Close();
                    ms.Dispose();
                    wpb.Path = savePath;
                }
                

                OnDataChanged(this, new DataChangeEventArgs(this.GetType().Name));

                preWarnId = warnId;
                //压缩视频
                if (preMinute != warnTime.Minute)
                {
                    count = 0;
                }
                preMinute = warnTime.Minute;
            }
        }

        #endregion

        protected void OnDataChanged(object sender, DataChangeEventArgs e)
        {
            if (this.DataChange != null)
            {
                DataChange(sender, e);
            }
        }

        protected DateTime GetLocalTime(int second)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(second).ToLocalTime();
        }

    }
}
